﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Songhay.Wpf.Mvvm
{
    using System.Resources;
    using Songhay.Globalization;
    using Songhay.Models;
    using System.Windows;

    /// <summary>
    /// Static members for WPF Applications
    /// </summary>
    public static class ApplicationUtility
    {
        /// <summary>
        /// Gets the Resource display text.
        /// </summary>
        /// <param name="text">The text.</param>
        public static string GetResourceDisplayText(string text)
        {
            text = Uri.UnescapeDataString(text)
                .Split('/').Last()
                .Replace(".s", string.Empty)
                .Replace(" - ", ": ");
            text = TextInfoUtility.ToTitleCase(text);

            return text;
        }

        /// <summary>
        /// Loads the resource.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <returns>A <see cref="System.String"/> representation of the Resource.</returns>
        public static string LoadResource(Uri uri)
        {
            var appStream = Application.GetResourceStream(uri);
            using(var reader = new StreamReader(appStream.Stream))
            {
                var s = reader.ReadToEnd();
                return s;
            }
        }

        /// <summary>
        /// Lists the packed resources.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        public static IEnumerable<PackedResource> ListPackedResources(Assembly dll)
        {
            var list = new List<PackedResource>();

            if(dll == null) return list;

            var simpleAsmName = new AssemblyName(dll.FullName).Name;
            var resourceName = simpleAsmName + ".g.resources";

            Stream stream = null;
            try
            {
                stream = dll.GetManifestResourceStream(resourceName);

                using(var reader = new ResourceReader(stream))
                {
                    var sb = new StringBuilder();
                    foreach(DictionaryEntry entry in reader)
                    {
                        var key = entry.Key.ToString();

                        if(key.StartsWith("packedxaml/"))
                        {
                            var resource =
                                new PackedResource
                                {
                                    DisplayName = ApplicationUtility
                                        .GetResourceDisplayText(key),

                                    Key = sb.AppendFormat(
                                        "/{0};Component/{1}",
                                            simpleAsmName, key).ToString()
                                };
                            list.Add(resource);
                            sb.Clear();
                        }
                    }
                }
            }
            finally
            {
                if(stream != null) stream.Dispose();
            }

            return list.OrderBy(i => i.Key);
        }
    }
}
